package cn.andev.app.morsesignal;

import java.util.ArrayList;

public class MorseCode {
	
	private static final int SHORT_INTERVAL = 330; //ms
	private static final int LONG_INTERVAL = 2000; //ms	
	private static final int SHORT_SIGNAL = 300; //ms
	private static final int LONG_SIGNAL = 900; //ms
	private static final int LONGLONG_INTERVAL =LONG_INTERVAL*3;
	
	/**
	A ・- 
	B -・・・ 
	C -・-・ 
	D -・・ 
	E ・ 
	F ・・-・ 
	G --・ 
	H ・・・・ 
	I ・・ 
	J ・--- 
	K -・- 
	L ・-・・ 
	M -- 
	N -・ 
	O --- 
	P ・--・ 
	Q --・- 
	R ・-・ 
	S ・・・ 
	T - 
	U ・・- 
	V ・・・- 
	W ・-- 
	X -・・- 
	Y -・-- 
	Z --・・ 
	
	？ ・・--・・ 
	/ -・・-・ 
	- -・・・・- 
	 
	0 ----- 
	1 ・---- 
	2 ・・--- 
	3 ・・・-- 
	4 ・・・・- 
	5 ・・・・・ 
	6 -・・・・ 
	7 --・・・ 
	8 ---・・ 
	9 ----・ 
	*/
	
	// A ・- 
	public static final int[] A = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL	
	};

	// B -・・・
	public static final int[] B = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL		
	};

	// C -・-・
	public static final int[] C = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// D -・・
	public static final int[] D = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// E ・
	public static final int[] E = new int[]{
		LONG_SIGNAL
	};

	// F ・・-・
	public static final int[] F = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// G --・
	public static final int[] G = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// H ・・・・
	public static final int[] H = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// I ・・
	public static final int[] I = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// J ・---
	public static final int[] J = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// K -・-
	public static final int[] K = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// L ・-・・
	public static final int[] L = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL, 
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// M --
	public static final int[] M = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// N -・
	public static final int[] N = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// O ---
	public static final int[] O = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// P ・--・
	public static final int[] P = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// Q --・-
	public static final int[] Q = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// R ・-・
	public static final int[] R = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// S ・・・
	public static final int[] S = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// T -
	public static final int[] T = new int[]{
		LONG_SIGNAL
	};

	// U ・・-
	public static final int[] U = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// V ・・・-
	public static final int[] V = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// W ・--
	public static final int[] W = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// X -・・- 
	public static final int[] X = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// Y -・--
	public static final int[] Y = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// Z --・・
	public static final int[] Z = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL	
	};

	// 0 ----- 
	public static final int[] Num0 = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	// 1 ・---- 
	public static final int[] Num1 = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	// 2 ・・--- 
	public static final int[] Num2 = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	// 3 ・・・--
	public static final int[] Num3 = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	// 4 ・・・・-
	public static final int[] Num4 = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	// 5 ・・・・・ 
	public static final int[] Num5 = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// 6 -・・・・
	public static final int[] Num6 = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// 7 --・・・
	public static final int[] Num7 = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// 8 ---・・ 
	public static final int[] Num8 = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// 9 ----・
	public static final int[] Num9 = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// ？ ・・--・・ 
	public static final int[] QuestionMark = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	// / -・・-・ 
	public static final int[] Slash = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	// - -・・・・- 
	public static final int[] Dash = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	// “.”：·—·—·—
	public static final int[] Point = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	//“:”：———···
	public static final int[] Colon = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	//“,”：——··——
	public static final int[] Comma = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	//“;”：—·—·—·
	public static final int[] Semicolon = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	//“=”：—···—
	public static final int[] EqualSign = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	//“'”：·————·
	public static final int[] Quotation = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	//“!”：—·—·——
	public static final int[] Exclamatory = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};
	
	//“_”：··——·—
	public static final int[] Underline = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	//“"”：·—··—·
	public static final int[] DoubleQuotation = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	//“(”：—·——·
	public static final int[] LeftParenthesis = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	//“)”：—·——·—
	public static final int[] RightParenthesis = new int[]{
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	//“$”：···—··—
	public static final int[] Dollar = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL
	};

	//“&”：· ···
	public static final int[] And = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_INTERVAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};

	//“@”：·——·—·
	public static final int[] At = new int[]{
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL,
		SHORT_INTERVAL,
		LONG_SIGNAL,
		SHORT_INTERVAL,
		SHORT_SIGNAL
	};
	
	//Blankspace
	public static final int[] Blankspace = new int[]{
		SHORT_INTERVAL,
		SHORT_INTERVAL,
		SHORT_INTERVAL
	};
	
	//Encode single char to morse code
	public static int[] encodeChar(char ch){
		switch(ch){
		case 'A':
			return A;
		case 'B':
			return B;
		case 'C':
			return C;
		case 'D':
			return D;
		case 'E':
			return E;
		case 'F':
			return F;
		case 'G':
			return G;
		case 'H':
			return H;
		case 'I':
			return I;
		case 'J':
			return J;
		case 'K':
			return K;
		case 'L':
			return L;
		case 'M':
			return M;
		case 'N':
			return N;
		case 'O':
			return O;
		case 'P':
			return P;
		case 'Q':
			return Q;
		case 'R':
			return R;
		case 'S':
			return S;
		case 'T':
			return T;
		case 'U':
			return U;
		case 'V':
			return V;
		case 'W':
			return W;
		case 'X':
			return X;
		case 'Y':
			return Y;
		case 'Z':
			return Z;
		case '0':
			return Num0;
		case '1':
			return Num1;
		case '2':
			return Num2;
		case '3':
			return Num3;
		case '4':
			return Num4;
		case '5':
			return Num5;
		case '6':
			return Num6;
		case '7':
			return Num7;
		case '8':
			return Num8;
		case '9':
			return Num9;
		case '?':
			return QuestionMark;
		case '/':
			return Slash;
		case '-':
			return Dash;
		case '.':
			return Point;
		case ':':
			return Colon;
		case ',':
			return Comma;
		case ';':
			return Semicolon;
		case '=':
			return EqualSign;
		case '\'':
			return Quotation;
		case '_':
			return Underline;
		case '!':
			return Exclamatory;
		case '"':
			return DoubleQuotation;
		case '(':
			return LeftParenthesis;
		case ')':
			return RightParenthesis;
		case '&':
			return And;
		case '@':
			return At;
		case ' ':
			return Blankspace;
		default:
			return null;
		}
	}
	
	// SOS
	/*public static final int[] mSOSCode = new int[] {
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			SHORT_SIGNAL,	// ...	S
			SHORT_INTERVAL,
			SHORT_SIGNAL,
			//
			LONG_INTERVAL,
			//
			LONG_SIGNAL,
			SHORT_INTERVAL,
			LONG_SIGNAL,	// ---	O
			SHORT_INTERVAL,
			LONG_SIGNAL,
			//
			LONG_INTERVAL,
			//
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			SHORT_SIGNAL,	// ...	S
			SHORT_INTERVAL,
			SHORT_SIGNAL,
			//
			LONGLONG_INTERVAL // dark
	};*/
	public static final int[] mSOSCode = encodeCustomMsg("SOS");
		
	// I love U
	/*public static final int[] mILoveUCode = new int[] {
			SHORT_SIGNAL,
			SHORT_INTERVAL,	// ..	I
			SHORT_SIGNAL,	
			//
			LONG_INTERVAL,
			//
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			LONG_SIGNAL,	 
			SHORT_INTERVAL, // .-..  L
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			SHORT_SIGNAL,			
			//
			LONG_INTERVAL,
			//
			LONG_SIGNAL,
			SHORT_INTERVAL,
			LONG_SIGNAL,	// ---	O
			SHORT_INTERVAL,
			LONG_SIGNAL,
			//
			LONG_INTERVAL,
			//
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			SHORT_SIGNAL,	// ...-	V
			SHORT_INTERVAL,
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			LONG_SIGNAL,
			//
			LONG_INTERVAL,
			//
			SHORT_SIGNAL,	// . E
			//
			LONG_INTERVAL,
			//
			SHORT_SIGNAL,
			SHORT_INTERVAL,
			SHORT_SIGNAL,	// ..-	U
			SHORT_INTERVAL,
			LONG_SIGNAL,		
			//
			LONGLONG_INTERVAL // dark
	};*/
	public static final int[] mILoveUCode = encodeCustomMsg("ILoveU"); 
		
	// Custom msg encoding
	public static int[] encodeCustomMsg(String strMsg){
		char[] charArray = strMsg.toUpperCase().toCharArray();
		ArrayList<Integer> code = new ArrayList<Integer>();
		
		for(int i=0;i<charArray.length;i++){
			int[] charCode = encodeChar(charArray[i]);
			ArrayList<Integer> tempCode = arrayToList(charCode);
			code.addAll(tempCode);
			
			// between two chars, insert a long interval; in the end, insert a longlong interval
			if(i != charArray.length-1)
				code.add(new Integer(LONG_INTERVAL));
			else
				code.add(new Integer(LONGLONG_INTERVAL));
		}
		return listToArray(code);
	}
	
	public static ArrayList<Integer> arrayToList(int[] array){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i=0;i<array.length;i++){
			list.add(new Integer(array[i]));
		}
		return list;
	}
	
	public static int[] listToArray(ArrayList<Integer> list){
		Integer[] array = (Integer[]) list.toArray();
		int[] arrayInt = new int[array.length];
		for(int i=0;i<array.length;i++){
			arrayInt[i] = array[i].intValue();
		}
		return arrayInt;
	}
}
